package com.raidroid.rdmute;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class RDMute extends Activity {
	
	ImageView StatusImage;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);           
        
        StatusImage = (ImageView)findViewById(R.id.ImageView03);
        startProcess();
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	startProcess();
    }
    
    private void startProcess(){
    	if(RDMuteService.IsServiceRunning){
        	StatusImage.setImageResource(R.drawable.kureyesil);
        }else{        	
        	StatusImage.setImageResource(R.drawable.kurekirmizi);
        }
        
    }
    
    private void DisplayRequest(String title, String str){   	
    	final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.resultdialog);
        dialog.setTitle(title);
        TextView qr = (TextView) dialog.findViewById(R.id.QueryResult);
        qr.setText(str);
        Button DialogClose = (Button) dialog.findViewById(R.id.btnClose);
        DialogClose.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
        dialog.show();
    }
    
    private String GetAboutFromTtx(){
    	String str = "";
    	InputStream is = getResources().openRawResource(R.raw.rdmuteabout);
    	if (is != null) {
    		StringBuilder sb = new StringBuilder();
    		String line;
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    		    while ((line = reader.readLine()) != null) {
    		    	sb.append(line).append("\n");
    		    }} catch (IOException e) {
				} finally {
    		    	try {
						is.close();
					} catch (IOException e) {
					}
    		    }
					PackageInfo info = null;
					try {
						info = getPackageManager().getPackageInfo(getPackageName(), 0);
					} catch (NameNotFoundException e) {}
    		    	str = sb.toString();
    		    	str = str.replace("##versionname##", info.versionName);
    		    }
    	return str;
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item){    	
		if(item.hasSubMenu() == false){			
			if (item.getOrder() == 0){
				switch(item.getItemId()){
				case R.id.item01:
					DisplayRequest(getString(R.string.About_RD_Mute), GetAboutFromTtx());
					break;
				case R.id.item02:
					Intent i = new Intent(RDMute.this, Preferences.class);
					startActivity(i);
					break;
				}				
			}
		}
    	return true;
    }
}